package Thread;

/**
 * 互斥锁
 */
public class SyncDemo4 {
    public static void main(String[] args) {
        Boo boo = new Boo();
        Thread t1 = new Thread(){
            @Override
            public void run() {
                boo.methodA();
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                boo.methodB();
            }
        };
        t1.start();
        t2.start();


    }
}
class Boo{
    public synchronized void methodA(){
        try {
            System.out.println(Thread.currentThread().getName()+"正在执行A方法。。。。");
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName()+"A方法执行完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //public synchronized void methodB(){
    public synchronized void methodB(){
        synchronized (this){
            try {
                System.out.println(Thread.currentThread().getName()+"正在执行B方法。。。。");
                Thread.sleep(2000);
                System.out.println(Thread.currentThread().getName()+"B方法执行完毕");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}